package com.garen.sys.web;

import com.garen.sys.biz.IFireApkBiz;
import com.garen.sys.entity.FireApk;
import org.springframework.stereotype.Controller;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import com.garen.common.*;
import org.springframework.web.servlet.ModelAndView;
import com.garen.sys.filter.SysFilter;
import com.garen.utils.CommonUtils;
import com.garen.utils.FileUtils;


@Controller
@RequestMapping("/sys")
public final class FireApkAction extends BaseAction{

	@Autowired
	private IFireApkBiz iFireApkBiz;

	@RequestMapping("/fireapk/json") 
	public ModelAndView json(PageBean<FireApk> pb,FireApk fireApk) {
		iFireApkBiz.getFireApkList(pb,fireApk);
		return Json(pb);
	}

	@RequestMapping("/fireapk/down") 
	public ModelAndView down(FireApk fireApk) {
		MsgBean mbean = SysFilter.getMsgBean(-1,"apk异常");
		FireApk apk = iFireApkBiz.getFireApk(fireApk);
		if(apk !=null) {
			String path = CommonUtils.getRealPath(apk.getApkPath());
			log.debug(path);
			byte[] body = FileUtils.readData(path);
			if(body != null) {
				return downFile(body, BaseAction.HTTP_MIME_TYPE_APK, apk.getApkName() + ".apk");
			}
		}
		return Json(mbean);
	}
	
	@RequestMapping("/fireapk/last") 
	public ModelAndView last(FireApk fireApk) {
		MsgBean mbean = SysFilter.getMsgBean();
		mbean.setObj(iFireApkBiz.getLastApk(fireApk));
		return Json(mbean);
	}
	
	@RequestMapping("/fireapk/boxJson") 
	public ModelAndView boxJson(FireApk fireApk) {
		return Json(iFireApkBiz.getFireApkList(fireApk));
	}

	@RequestMapping("/fireapk/edit") 
	public ModelAndView edit(ModelAndView mav,FireApk fireApk) {
		mav.setViewName("sys/FireApkEdit");
		if(fireApk.getApkId() != null){
			fireApk=iFireApkBiz.getFireApk(fireApk);
		}
		mav.addObject("fireApk",fireApk);
		return mav;
	}

	@RequestMapping("/fireapk/save") 
	public ModelAndView save(FireApk fireApk) {
		MsgBean mbean = SysFilter.getMsgBean(0, "保存成功 !");
		iFireApkBiz.saveFireApk(mbean,fireApk);
		return Json(mbean);
	}

	@RequestMapping("/fireapk/del") 
	public ModelAndView del(FireApk fireApk) {
		MsgBean mbean = SysFilter.getMsgBean(0, "删除成功 !");
		iFireApkBiz.delFireApk(mbean,fireApk);
		return Json(mbean);
	}

}
